package com.tedu.aopdemo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class SysoutAspect {
//    @Before("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
//    public void before(JoinPoint joinPoint){
////        System.out.println("target"+joinPoint.getTarget());
////        System.out.println(joinPoint.getSignature().getName());
////        System.out.println("参数:"+ Arrays.toString(joinPoint.getArgs()));
//
//        String methodName= joinPoint.getSignature().getName();
//        String className = joinPoint.getTarget().getClass().getSimpleName();
//        String args = Arrays.toString(joinPoint.getArgs());
//        System.out.println("开始调用"+className+"."+methodName+"(),实参为:"+args);
//    }
//
//    @AfterReturning(
//            value = "execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))",
//            returning = "returnValue"
//    )
//    public void afterReturning(JoinPoint joinPoint,Object returnValue){
//        String methodName = joinPoint.getSignature().getName();
//        String className = joinPoint.getTarget().getClass().getSimpleName();
//        System.out.println(className+"."+methodName+"()执行完毕,返回值为:"+returnValue);
//    }
//
//    @After("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
//    public void after(JoinPoint joinPoint){
//        String methodName = joinPoint.getSignature().getName();
//        String className = joinPoint.getTarget().getClass().getSimpleName();
//        System.out.println(className+"."+methodName+"()完毕！");
//    }
//
//    @AfterThrowing(
//            value = "execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))",
//            throwing = "e"
//    )
//    public void AfterThrowing(JoinPoint joinPoint,Exception e){
//        String methodName = joinPoint.getSignature().getName();
//        String className = joinPoint.getTarget().getClass().getSimpleName();
//        System.out.println(className+"."+methodName+"()抛出异常了:"+e.getMessage());
//    }


    @Around("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println(className + "." + methodName+"()开始执行,参数为:"+args);
        try{
            Object returnValue = joinPoint.proceed();
            System.out.println(className+"."+methodName+"()执行完毕");
            return returnValue;
        }catch (Throwable e){
            System.out.println(className+"."+methodName+"()出现异常:"+e.getMessage());
            throw new RuntimeException(e);
        }finally {
            System.out.println(className+"."+methodName+"()最终执行完毕");
        }
    }
}
